<?php
/* 
* RelatedContent 1.0
* Resource Type: Snippet
* Author: atma
* Description: Display related content using ids form TV.
* Dated 2009-03-28
* Use: [!RelatedContent? &TVName=`RelatedContentIDs` &mode=`content` &outerClass=`related-content`!] where &RelatedContentIDs is name of TV which contain comma delimeted document IDs; &mode is output mode: content|link; &outerClass - class for outer container (div for content, ul for link).
*/

$output = '';
isset($TVName) ? $TVName : $TVName = 'RelatedContentIDs';
isset($mode) ? $mode : $mode = 'content';
isset($outerClass) ? $outerClass : $outerClass = 'related-content';

$id = $modx->documentObject['id'];

$TVContent = $modx->getTemplateVarOutput($TVName,$id,1);
$relatedIDs = explode(',', $TVContent[$TVName]);

if ($mode == 'content') {
    $output .= '<div class="'. $outerClass .'">';
    foreach ($relatedIDs as $relatedID) {
        $relatedID = trim($relatedID);
        $relatedContent = $modx->getDocument($relatedID,$fields='pagetitle,longtitle,content');
        $output .= '
        <div>'. $relatedContent['content'] .'</div>
';
    }
    $output .= '</div>';
} elseif ($mode == 'link') {
    $output .= '<ul class="'. $outerClass .'">';
    foreach ($relatedIDs as $relatedID) {
        $relatedID = trim($relatedID);
        $relatedContent = $modx->getDocument($relatedID,$fields='pagetitle,longtitle');
        $output .= '
        <li>
            <a href="'. $modx->makeUrl($relatedID, '', '', $scheme= 'full') .'" title="'. $relatedContent['longtitle'] .'" >'. $relatedContent['pagetitle'] .'</a>
        </li>
        ';
    }
    $output .= '</ul>';
}

return $output;
?>
